var loadedResourceSset = new Set();

export function loadScript(url,id) {
    return new Promise(resolve =>{
        // 检查页面中是否已经加载了相同的CSS文件
        if (loadedResourceSset.has(id)) {
            resolve()
            return;
        }

        var script = document.createElement("script");
        script.type = "text/javascript";

        if (script.readyState) {  // IE
            script.onreadystatechange = function() {
                if (script.readyState == "loaded" || script.readyState == "complete") {
                    script.onreadystatechange = null;
                    resolve();
                }
            };
        } else {  // Others
            script.onload = function() {
                resolve();
            };
        }

        // 将ID添加到已加载的集合中
        loadedResourceSset.add(id);
        script.src = url;
        document.getElementsByTagName("head")[0].appendChild(script);
    } )

}

/*
* loadCss("https://example.com/style.css");

* */
export function loadCss(url,id) {
    // 检查页面中是否已经加载了相同的CSS文件
    if (loadedResourceSset.has(id)) {
        return;
    }

    var link = document.createElement("link");
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = url;
    document.getElementsByTagName("head")[0].appendChild(link);
    // 将ID添加到已加载的集合中
    loadedResourceSset.add(id);
}